package org.dtrd.modules.patient.entity.bean.report;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.common.util.DateUtils;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.evaluation.entity.bean.report.TotalEvaluationReports;
import org.dtrd.modules.patient.entity.po.DtrdEntRdReport;
import org.dtrd.modules.record.entity.bean.PeriodBloodPressure;
import org.dtrd.modules.record.entity.bean.PeriodBmi;
import org.dtrd.modules.record.entity.bean.SugarChart;
import org.dtrd.modules.suggest.entity.bean.PeriodFoodScore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author qr
 * @date 2022/3/31 14:08
 */
@ApiModel("总结报告")
@Getter
@Setter
@Slf4j
public class ReportSummary {

    @ApiModelProperty("报告的id")
    private Integer dataId;

    @ApiModelProperty("报告名字")
    private String reportName;

    @ApiModelProperty("报告创建时间")
    @JsonFormat(pattern = Constant.TIME_PATTERN_SDF, timezone = Constant.TIME_ZONE_1)
    private Date reportCreateTime;

    @ApiModelProperty("报告编号")
    private String reportId;

    @ApiModelProperty("患者姓名")
    private String patientsName;

    @ApiModelProperty("患者性别")
    private String patientsSex;

    @ApiModelProperty("患者年龄")
    private Integer patientAge;

    @ApiModelProperty("总结报告")
    public SummaryReportContent reportContent = new SummaryReportContent();

    @ApiModelProperty("总结报告输入数据")
    public ReportSummaryUpdateRequest.ReportSummaryInputUpdate summaryInputUpdate = new ReportSummaryUpdateRequest.ReportSummaryInputUpdate();

    @ApiModelProperty("总结")
    private String summary;

    @ApiModelProperty("发布状态")
    private Integer publishStatus;
    @ApiModelProperty("发布状态文本")
    private String publishStatusText;

    @ApiModelProperty("ogtt chart-服务前")
    private Map<String, String[]> ogttChartFirst;
    @ApiModelProperty("ogtt chart-服务后")
    private Map<String, String[]> ogttchartSummary;

    @ApiModelProperty("创建人")
    private String creator;

    @ApiModel("总结报告内容")
    @Getter
    @Setter
    public static class SummaryReportContent {

        @ApiModelProperty("基本信息")
        private PatientBasicInfo basicInfo;

        @ApiModelProperty("服务方案")
        private ReportServiceProgramme serviceProgramme;

        @ApiModelProperty("打卡总结")
        private ReportTable signinSummary;

        @ApiModelProperty("打卡和达标率")
        private SigninRateLine signinAndReachStandardRate;

//        @ApiModelProperty("控制目标-已弃用")
//        private ReportTable targets;

        @ApiModelProperty("患者档案-服务前")
        private PatientArchive archiveFirst;
        @ApiModelProperty("患者档案-服务后")
        private PatientArchive archiveSummary;

        @ApiModelProperty("量表评估-服务前")
        private TotalEvaluationReports evaluationFirst;
        @ApiModelProperty("量表评估-服务后")
        private TotalEvaluationReports evaluationSummary;

        @ApiModelProperty("用药调整")
        private List<ReportDrugSuggest> drugAdjust;

        @ApiModelProperty("饮食得分")
        private PeriodFoodScore periodFoodScore;

        @ApiModelProperty("日常监测趋势血糖模块（空腹与餐后血糖）")
        private SugarChart sugarChart;

        @ApiModelProperty("日常监测趋势血压模块")
        private PeriodBloodPressure periodBloodPressure;

        @ApiModelProperty("日常监测趋势体重模块")
        private PeriodBmi periodBmi;

        @ApiModelProperty("控制目标（新）")
        private List<ControllerTargetReport> controllerTargetReportList;

        @ApiModelProperty("打卡率和达标率")
        private SigninRateLine signinRateLine;

    }

    public ReportSummary parseFromPO(DtrdEntRdReport po) {
        this.dataId = po.getDataId();
        this.reportName = po.getReportName();
        this.reportCreateTime = po.getCreateTime();
        this.patientsName = JSONUtil.toBean(po.getPatientArchive(), SummaryReportContent.class).getBasicInfo().getPatientsName();
        this.patientsSex = JSONUtil.toBean(po.getPatientArchive(), SummaryReportContent.class).getBasicInfo().getPatientsSex();
        this.patientAge = JSONUtil.toBean(po.getPatientArchive(), SummaryReportContent.class).getBasicInfo().getPatientAge();
        this.reportId = DateUtils.date2Str(po.getCreateTime(),DateUtils.short_date_sdf_ym.get()) + po.getDataId().toString();
        this.summaryInputUpdate = JSONUtil.toBean(po.getPatientArchiveReport(), ReportSummaryUpdateRequest.ReportSummaryInputUpdate.class);
        this.summary = po.getSummary();
        this.publishStatus = po.getPublishStatus();
        this.publishStatusText = Constant.ReportPublishType.valueOf(po.getPublishStatus()).getName();
        this.reportContent = JSONUtil.toBean(po.getPatientArchive(), SummaryReportContent.class);
        SigninRateLine signinRateLine = reportContent.getSigninAndReachStandardRate();
        if (signinRateLine != null) {
            List<List<String>> dataList = signinRateLine.getDataList();
            if (dataList == null) {
                dataList = new ArrayList<>(2);
            }
            List<String> punchRate = signinRateLine.getPunchRate();
            List<String> punchRateList = new ArrayList<>(10);
            List<String> complianceRate = signinRateLine.getComplianceRate();
            List<String> complianceRateList = new ArrayList<>(10);

            if (punchRate != null) {
                punchRate.forEach(item->{
                    if (item!= null && item.contains("%")){
                        item = item.replaceAll("%","");
                        punchRateList.add(item);
                    }
                });
            }
            if (complianceRate != null) {
                complianceRate.forEach(item->{
                    if (item!= null && item.contains("%")){
                        item = item.replaceAll("%","");
                        complianceRateList.add(item);
                    }
                });
            }
            dataList.add(punchRateList);
            dataList.add(complianceRateList);
            signinRateLine.setDataList(dataList);
        }
        return this;
    }

}
